;;; Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
;;; This file is part of rehash.
;;;
;;; rehash is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; rehash is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with rehash.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (rehash)
             (rehash fibers)
             (fibers)
             (rehash configuration)
             (system foreign)
             (srfi srfi-64)
             (rnrs base))

;; TODO
(define (startup)
  (system* "gnunet-arm" "-c" "scheme-tests/nonetconf.conf"
           "--logfile" "/dev/stdout"
           "--start"))
(define (stop)
  (system* "gnunet-arm" "-c" "scheme-tests/nonetconf.conf"
           "--end"))

(define *test-config* #f)

(define (first-task _)
  (define h1 (bytevector->hash HASH_SHA256 #vu8(0 1) 0 2))
  (define h2 (bytevector->hash HASH_SHA256 #vu8(2 1) 0 2))
  (define s #f)
  (define s1 #f)
  (define s2 #f)
  (define q1 #f)
  (startup)
  (test-begin "service-api-smoke")
  (display "opening connection to rehash service")
  (newline)
  (set! s (open-rehash-service *test-config*))
  (while (not (rehash-service-connected? s))
    (display "waiting on rehash service")
    (newline)
    (sleep 1)
    (rehash-service-retry-connect))
  (display "done!")
  (newline)
  ;; FIXME wait until service is up
  (set! s1 (store-hash! s h1 h2
                        #:expiration-time (expt 2 48)
                        #:anonymity-level 0))
  (display "started storing s1!")
  (newline)
  (set! s2 (store-hash! s h2 h1
                        #:expiration-time 345678
                        #:anonymity-level 0))

  (display "started storing s2!")
  (newline)
  ;; todo higher anonymity levels
  (store-abort! s1)
  (display "stopped storing s1!")
  (newline)
  (set! q1 (query! s h1 HASH_SHA3_512
                   #:anonymity-level 0
                   #:options 0
                   #:when-found display))
  (display "started querying q1!")
  (newline)
  (store-abort! s2)
  (display "stopped storing s2!")
  (newline)
  (query-abort! q1)
  (display "stopped querying s2!")
  (newline)
  (close-rehash-service s)
  (display "closed rehash service")
  (test-end "service-api-smoke")
  (stop))

(define (main args)
  (set! *test-config* (load-configuration "scheme-tests/nonetconf.conf"))
  #; (assert (not (null-pointer? *test-config*)))
  (run-fibers (lambda ()
                (init-fibers-scheduler!)
                (start-fibers-scheduler!)
                (spawn-fiber
                 (lambda ()
                   (do-gnunet-stuff (lambda ()
                                      (first-task #f)))))
                ;; give the previous time to complete
                ;; TODO
                (sleep 10))))
#;(call-with-scheduler *test-config* first-task)
